package P150;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author LiangMu
 * @version \$Id: P29.java, v 0.1, 2024年07月05日 18:18 LiangMu Exp $$
 */
public class P29 {

    public static int lengthOfLongestSubstring(String s) {
        if (s.isEmpty()) {
            return 0;
        }
        char[] chars = s.toCharArray();
        int left = 0;
        int res = 1;
        Set<Character> set = new HashSet<>();
        set.add(chars[0]);
        for (int i = 1; i < chars.length; i++) {
            if (set.contains(chars[i])) {
                for (; left < i; left++) {
                    set.remove(chars[left]);
                    if (chars[left] == chars[i]) {
                        ++left;
                        break;
                    }
                }
            }
            set.add(chars[i]);
            res = Math.max(res, set.size());
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("tmmzuxt"));
        System.out.println(lengthOfLongestSubstring("bbbbb"));
        System.out.println(lengthOfLongestSubstring("pwwkew"));
    }

}
